//常用方法集合
export default {
	/**
	 * title(标题)，
	   icon(图标),  success，loading，none
	   duration(延时) 0为不关闭, 毫秒数
	   mask 	是否显示透明蒙层，防止触摸穿透，默认：false
	 * toast提示
	 */
	toast(title, icon = 'none', duration = 3000, mask = false, callback = () => {}) {
		if (Boolean(title) === false) {
			return;
		}
		uni.showToast({
			title,
			duration,
			mask,
			icon,
			// 接口调用成功的回调函数
			success() {
				setTimeout(callback, duration)
			}
		});
	},


	isNUll(data) {
		if (data != null) {
			data = data.toString();
			data = data.replace(/\s\s*$/, '');
			if (data == '') {
				return true;
			} else if (data.length == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	},
	/**
	 * 获取可用窗口的高度
	 */
	getWindowHeight() {
		let height;
		uni.getSystemInfo({
			success: (res) => {
				height = res.windowHeight;
				height = height * uni.getStorageSync('rpx');
				console.log(height);
				console.log(res.windowHeight);
			}
		});
		return height;
	},

	/**
	 * @param {Object} options
	 * status 返回的状态
	 * success 请求成功执行的函数
	 * successMsg 请求成功的提示
	 * fail 请求失败的执行的函数
	 * failsMsg 请求失败的提示
	 *
	 */
	requestMsg(options) {
		let defaultData = {
			status: options.data.data.status,
			success: options.success,
			successMsg: options.successMsg ? options.successMsg : "操作成功",
			fail: options.fail,
			failsMsg: options.failsMsg ? options.failsMsg : options.data.data.msg,
			isGetData: options.isGetData,
		}
		if (defaultData.status == 0) {
			if (defaultData.success != null) {
				defaultData.success(options.data.data.data);
			}
			if (defaultData.isGetData) {
				return options.data.data.data;
			} else {
				this.toast(defaultData.successMsg);
			}
		} else {
			this.toast(defaultData.failsMsg);
			if (defaultData.fail != null) {
				defaultData.fail(options.data.data.data);
			}
		}
	},

	responseData(res, callback = () => {}) {
		if (res.data.status == 0) {
			return res.data.data;
		} else {
			this.toast(res.data.msg);
			callback();
		}
	},

	//时间显示
	simpleDuration(duration, type) {

		if (type === 's') {
			duration = duration * 1000
		}
		let str = ''
		let days = '',
			hours = '',
			minutes = '',
			seconds = ''
		let day = 24 * 60 * 60 * 1000,
			hour = 60 * 60 * 1000,
			minute = 60 * 1000,
			second = 1000
		if (duration >= day) {
			days = Math.floor(duration / day) + '天'
			hours = Math.floor(duration % day / hour) + '小时'
		} else if (duration >= hour && duration < day) {
			hours = Math.floor(duration / hour) + '小时'
		} else if (duration > minute && duration < hour) {
			minutes = Math.floor(duration / minute) + '分钟'
		} else if (duration < minute) {
			seconds = Math.floor(duration / second) + '秒'
		}
		return days + hours + minutes + seconds
	},

	compressImg(file) {
		return new Promise((resolve, reject) => {
			let reader = new FileReader();
			reader.onloadend = function(e) {
				let img = new Image();
				img.onload = function() {
					let canvasWidth = img.width // 图片原始长宽
					let canvasHeight = img.height
					let canvas = document.createElement('canvas');
					let ctx = canvas.getContext('2d');
					// 创建固定750宽度的图片，可自定义大小
					canvas.width = 750;
					canvas.height = (750 * canvasHeight) / canvasWidth;
					ctx.drawImage(img, 0, 0, 750, (750 * canvasHeight) / canvasWidth);
					canvas.toBlob(
						function(blob) {
							// 创建file文件
							let pressFile = new File([blob], file.name, {
								type: file.type
							})
							Object.defineProperty(pressFile, 'path', {
								get: () => URL.createObjectURL(blob)
							})
							return resolve(pressFile)
						},
						file.type,
						0.9 // 图片质量，值0-1之间，可不填默
					);
				};
				img.src = e.target.result;
			};
			// 读取 File 对象，然后触发 loadend 事件
			reader.readAsDataURL(file);
		})
	},

	/**
	 *
	 * @param {缓存key} key
	 * @param {需要存储的缓存值} value
	 * @param {过期时间，默认0表示永久有效} expire
	 */
	setCache (key, value, expire = 0) {
		let obj = {
			data: value, //存储的数据
			time: Date.now() / 1000, //记录存储的时间戳
			expire: expire //记录过期时间，单位秒
		}
		uni.setStorageSync(key, obj)
	},
	/**
	 *
	 * @param {缓存key} key
	 */
	getCache(key) {
		let val = uni.getStorageSync(key)
		if (!val) {
			return null
		}
		if (val.expire && Date.now() / 1000 - val.time > val.expire) {
			uni.removeStorageSync(key)
			return null
		}
		return val.data
	},

	/**
	 * 获取链接中问号？后的参数例如www.baidu.com?id=1&name=666
	 * @param url
	 * @returns {{
	 *     id:1,
	 *     name:666
	 * }}
	 */
	getUrlParams(url) {
		var vars = {};
		var parts = url.replace(/[?&]+([^=&]+)=([^&#]*)/gi,
			function(m,key,value) {
				vars[key] = value;
			}
		);
		return vars;
	},
}
